/*
 * Author: 李波（Edwin Lee)
 * Date: 2024-06-18 19:22:07
 * FilePath: /chat/src/server/model/offlinemessagemodel.cc
 * LastEditTime: 2024-07-11 00:20:43
 * version: 1.0
 * Description: 
 */

#include "offlinemessagemodel.h"
#include "connection.h"
#include "commonConnectionPool.h"

void OfflineMsgModel::insert(int userid, string msg)
{
    //组装sql语句
    char sql[1024] = {0};
    sprintf(sql,"insert into offlinemessage value(%d, '%s')",userid, msg.c_str());
    ConnectionPool& cnp = ConnectionPool::getInstance();
    shared_ptr<Connection> conn = cnp.getConnection();
    if(conn != nullptr)
    {
        conn->update(sql);
    }
}

void OfflineMsgModel::remove(int userid)
{
    //组装sql语句
    char sql[1024] = {0};
    sprintf(sql,"delete from offlinemessage where userid = %d",userid);
    ConnectionPool& cnp = ConnectionPool::getInstance();
    shared_ptr<Connection> conn = cnp.getConnection();
    if(conn != nullptr)
    {
        conn->update(sql);
    }
}

vector<string> OfflineMsgModel::query(int userid)
{
    //组装sql语句
    char sql[1024] = {0};
    sprintf(sql,"select message from offlinemessage where userid=%d",userid);
    ConnectionPool& cnp = ConnectionPool::getInstance();
    shared_ptr<Connection> conn = cnp.getConnection();
    vector<string> vstr;
    if(conn != nullptr)
    {
        MYSQL_RES* res = conn->query(sql);
        if(res != nullptr)
        {
            MYSQL_ROW row;
            while ((row = mysql_fetch_row(res)) != nullptr)
            {
                vstr.push_back(row[0]);
            }
            mysql_free_result(res);
        }
    }
    return vstr;
}